// pages/backPerson/backPerson.js
var util = require("../../utils/util.js");
const APP = getApp()
Page({

  /**
   * 页面的初始数据
   */

  data: {

    pageNum: 1,
    pageSize: 4,
    sexName: '',
    option1: [{
        text: '最新看押',
        value: 0
      },
      {
        text: '一周前',
        value: 1
      },
      {
        text: '半月前',
        value: 2
      },
      {
        text: '一月前',
        value: 3
      },
    ],
    itemTitle: '选择日期',
    columns: ['男', '女', '未知'],
    value: '',
    value1: 0,
    currentDate: new Date().getTime(),

    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`;
      } else if (type === 'month') {
        return `${value}月`;
      }
      return value;
    },
    prisonList: [],
    statusType: [],
    form: {
      startDate: '',
      endDate: '',
      personId: '',
      deptId: '',
      card: '',
      prison: '',
      sex: '',
      remark: '',
      age: '',
      delFlag: '',
      createBy: '',
      createTime: '',
      updateBy: '',
      updateTime: '',

    },
    navList: [],
    show: false,
    show1: false,
    showsex: false,
    showprison: false,
    title: '',
    paramas: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getList();
    this.getPerson();
    this.getzidian();

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this
    that.setData({
      navList: []
    })
    that.getPersonList();
    // var list = {
    //   url: '/system/person/list?pageNum=' + this.data.pageNum + '&pageSize=' + this.data.pageSize,
    //   method: 'get',
    //   data: this.data.paramas
    // }
    // APP.request.backWxRequest(list).then(res => {
    //   if (res.data.code === 200) {
    //     this.setData({
    //       navList: res.data.rows
    //     })
    //   }
    // });
    // this.getzidian()
  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let that = this
    that.setData({
      pageNum: that.data.pageNum + 1
    })
    // 获取预约数组
    that.getPersonList();

  },
  getPersonList() {
    // var url
    // if (!!this.data.paramas.createTime && !!this.data.paramas) {
    //   url = '/system/person/list?pageNum=' + this.data.pageNum + '&pageSize=' + this.data.pageSize + '&startDate=' + this.data.paramas.createTime
    // } else {
    //   url = '/system/person/list?pageNum=' + this.data.pageNum + '&pageSize=' + this.data.pageSize
    // }


    var list = {
      url: '/system/person/list?pageNum=' + this.data.pageNum + '&pageSize=' + this.data.pageSize,
      // url: url,
      method: 'get',
      data: this.data.paramas

    }
    APP.request.backWxRequest(list).then(res => {
      if (res.data.code === 200) {

        if (this.data.pageNum == 1) {
          this.setData({
            navList: []
          })
        }

        var navList = this.data.navList
        var rows = res.data.rows
        var allArr = [...navList, ...rows]
        allArr.forEach((items, i) => {
          var statusName = APP.selectDictLabel(this.data.statusType, items.sex)
          items.statusName = statusName
          // console.log(statusName);
        })


        this.setData({
          navList: allArr
        })
      }
    });
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //字典
  getzidian: function () {
    APP.getbackDict('sys_user_sex').then(res => {
      this.setData({
        statusType: res.data.data
      })
      // 获取第一页预约数组  

    })

  },
  //监区列表
  getPerson: function () {

    var person = {
      url: '/system/dept/queryPrisons',
      method: 'get',

    }
    APP.request.backWxRequest(person).then(res => {
      if (res.data.code === 200) {
        this.setData({
          prisonList: res.data.data
        });
        console.log(this.data.prisonList)
      }
    });

  },
  //监听input
  name: function (e) {

    this.setData({
      ['form.name']: e.detail.value

    });
  },
  age: function (e) {

    this.setData({
      ['form.age']: e.detail.value
    });

  },
  prison: function (e) {

    this.setData({
      ['form.prison']: e.detail.value
    });

  },
  remark: function (e) {

    this.setData({
      ['form.remark']: e.detail
    });

  },
  card: function (e) {

    this.setData({
      ['form.card']: e.detail.value
    });

  },

  //加载信息
  getList: function () {


  },
  //搜索
  onClick: function () {
    this.setData({

      pageNum: 1,
      pageSize: 4,
      navList: []
    })
    this.onShow();
  },
  onChange(e) {

    this.setData({
      paramas: {
        name: e.detail,
      }
    });
  },
  onSearch() {

    this.setData({
      pageNum: 1,
      pageSize: 4,
      navList: []
    })
    this.onShow();
  },
  //记录
  jilu: function (detail) {
    this.setData({
      paramas: [],
      itemTitle: '选择日期'
    });
    console.log(detail.detail)
    this.setData({
      paramas: {
        timeNum: detail.detail
      }
    });
    this.onShow();
  },
  //日期

  sure: function (event) {
    this.setData({
      pageNum: 1,
    })
    var date = event.detail
    this.setData({
      startDate: '',
      itemTitle: util.js_date_time(date),
      paramas: {
        // createTime: util.js_date_time(date)
        startDate: util.js_date_time(date)
      }
    });
    // console.log(this.data.paramas.createTime);

    // this.getList();
    this.getPersonList()
    this.selectComponent('#item').toggle();
  },

  close: function () {
    this.selectComponent('#item').toggle();
  },
  //删除修改添加信息
  xiugai(e) {
    this.setData({
      paramas: [],
      show: true,
      title: "修改信息",
      paramas: {
        personId: e.currentTarget.dataset.personid
      }
    })
    var personList = {
      url: '/system/person/list',
      method: 'get',
      data: this.data.paramas
    }
    APP.request.backWxRequest(personList).then(res => {
      if (res.data.code === 200) {
        this.setData({
          form: res.data.rows[0],
          sexName: this.data.columns[res.data.rows[0].sex]
        });

      }
    });
  },
  shanchu(e) {
    let that = this
    // 确认框
    wx.showModal({
      title: '警告',
      content: '确认删除吗',
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定')
          var shanchuList = {
            url: '/system/person/' + e.currentTarget.dataset.personid,
            method: 'Delete',
          }
          APP.request.backWxRequest(shanchuList).then(res => {
            if (res.data.code === 200) {
              that.setData({
  
                pageNum: 1,
                pageSize: 4,
                navList: []
              });
              that.onShow();
            }
          });
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
     
      }
    })
  },



  tianjia() {
    this.setData({
      sexName: '',
      paramas: [],
      form: [],
      show: true,
      title: "添加信息",
    })
  },
  //提交
  confirm() {
    if (this.data.form.personId == undefined) {
      if (!!!this.data.form.name) {
        wx.showToast({
          title: '请填写姓名',
          image: '../../assets/error.png',
        })

      } else if (this.data.form.sex == undefined) {
        wx.showToast({
          title: '请选择性别',
          image: '../../assets/error.png',
        })
      } else if (!!!this.data.form.age) {
        wx.showToast({
          title: '请填写年龄',
          image: '../../assets/error.png',
        })
      } else if (!!!this.data.form.prison) {
        wx.showToast({
          title: '请选择监区',
          image: '../../assets/error.png',
        })
      } else if (!!!this.data.form.card) {
        wx.showToast({
          title: '请填写身份证',
          image: '../../assets/error.png',
        })
      } else {
        var personList = {
          url: '/system/person',
          method: 'post',
          data: this.data.form
        }
        APP.request.backWxRequest(personList).then(res => {
          if (res.data.code === 200) {

            this.setData({
              sexName: '',
              paramas: '',
              pageNum: 1,
              pageSize: 4,
              navList: []
            })
            this.onShow();
          }
        });
      }

    } else {
      var personList = {
        url: '/system/person',
        method: 'put',
        data: this.data.form
      }
      APP.request.backWxRequest(personList).then(res => {
        if (res.data.code === 200) {
          this.setData({
            sexName: '',
            paramas: '',
            pageNum: 1,
            pageSize: 4,
            navList: []
          });



          this.onShow();
        }
      });
    }

  },


  //性别
  showSex() {
    this.setData({
      show: false,
      showsex: true
    })
  },
  onConfirm(event) {
    console.log(event.detail)
    this.setData({
      ['form.sex']: event.detail.index,
      showsex: false,
      show: true,
      sexName: this.data.columns[event.detail.index]
    })
  },
  onCancel() {
    this.setData({
      showsex: false,
      show: true,
    })

  },
  //监区
  showPrison() {
    this.setData({
      showprison: true,
      show: false,
    })
  },
  onConfirm1(event) {
    console.log(event.detail)
    this.setData({
      ['form.prison']: event.detail.value,
      showprison: false,
      show: true,
    })
  },
  onCancel1() {
    this.setData({
      showprison: false,
      show: true,


    })
  },
})